﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EGym.BL.DataGestor;
using EGym.BL.Forms;
using EGym.Data;
using EGym.Models;

namespace EGym.Controllers
{
    public partial class AlunoController : BaseController
    {
        //
        // GET: /Aluno/
        public ActionResult Index()
        {
            if (SessionManager.EstaAutenticado())
            {
                AlunoGestor gestor = new AlunoGestor(new DBDataContext());
                AlunoForm form = new AlunoForm();

                form.GridMain.DataBind(gestor.GetAlunos(SessionManager.Usuario.AfiliadoId));

                SetUpGrid(form.GridMain);
                
                return View(form);
            }
            return RedirectToAction("SessaoExpirada", "Home");
        }
        //
        // POST: /Aluno/
        [HttpPost]
        public ActionResult Salvar(FormCollection formCollection)
        {
            AlunoGestor gestor = new AlunoGestor(new DBDataContext());
            AlunoForm form = new AlunoForm();
            form.Lista = gestor.GetAlunosPaginedList(SessionManager.Usuario.AfiliadoId, Constantes.PAGE_SIZE,
                                                     Constantes.PAGE_INDEX_STARTUP);

            UpdateModel(form, formCollection);
            if (form.Aluno.AlunoId.Equals(0))
                gestor.AdcionarAlunoAssociadoComAfiliacaoAssociada(SessionManager.Usuario.AfiliadoId,form.Aluno);

            return View(form);
        }

    }
}
